package com.myproject.sbp.apollo.conf;

import com.ctrip.framework.apollo.core.ConfigConsts;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.context.scope.refresh.RefreshScope;
import org.springframework.stereotype.Component;

/**
 * 情况二
 * 方式一
 * @author ywh
 */
@Slf4j
@Component
public class ApolloRefreshScopeConfig {
    private final TestBean2 testBean2;
    private final RefreshScope refreshScope;

    public ApolloRefreshScopeConfig(final TestBean2 testBean2, final RefreshScope refreshScope) {
        this.testBean2 = testBean2;
        this.refreshScope = refreshScope;
    }

    @ApolloConfigChangeListener(value = ConfigConsts.NAMESPACE_APPLICATION)
    public void onChange(ConfigChangeEvent changeEvent) {
        log.info("before refresh {}", testBean2.toString());
        refreshScope.refresh("sampleRedisConfig");
        log.info("after refresh {}", testBean2.toString());
    }

}